package cn.rlfit.gulimall.product.service.impl;

import cn.rlfit.common.utils.PageUtils;
import cn.rlfit.common.utils.Query;
import cn.rlfit.gulimall.product.dao.CategoryDao;
import cn.rlfit.gulimall.product.entity.CategoryEntity;
import cn.rlfit.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SuppressWarnings("all")
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @SuppressWarnings("all")
    public List<CategoryEntity> listWithTree() {
        //    查询出所有分类
        List<CategoryEntity> entitiesList = baseMapper.selectList(null);
        //        利用过滤器找到所有的一级分类
        List<CategoryEntity> categoryEntities = entitiesList.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid() == 0;
        }).map((menu) -> {
            menu.setChildren(getCategoryEntityChildrens(menu, entitiesList));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return categoryEntities;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
//        TODO 检查当前删除的菜单，是否被别的地方进行引用

//        逻辑删除
        baseMapper.deleteBatchIds(asList);
    }

    /**
     * 找到catelogId的完整路径
     *
     * @param groupId
     * @return
     */
    @Override
    public Long[] findCatelogPath(Long catelogId) {
        List<Long> path = new ArrayList<>();
        List<Long> parentPath = findParentPath(catelogId, path);
        Collections.reverse(parentPath);
        return (Long[]) parentPath.toArray(new Long[parentPath.size()]);
    }

    /**
     * 递归查找所有节点
     * @param groupId
     * @param path
     * @return
     */
    private List<Long> findParentPath(Long groupId,List<Long> paths) {
        //        按照层级关系找出id
        paths.add(groupId);
        CategoryEntity byId = this.getById(groupId);
        if (byId.getParentCid() != 0) {
            findParentPath(byId.getParentCid(), paths);
        }
        return paths;
    }

    private List<CategoryEntity> getCategoryEntityChildrens(CategoryEntity category, List<CategoryEntity> categoryEntityList) {
//        递归查找所有子菜单
        List<CategoryEntity> collect = categoryEntityList.stream().filter((cate) -> {
            return cate.getParentCid().equals(category.getCatId());
        }).map((menu) -> {
            menu.setChildren(getCategoryEntityChildrens(menu, categoryEntityList));
            return menu;
        }).sorted((menu1, menu2) -> {
            return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());
        return collect;
    }
}